﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using br.com.SistemaChamados;
using br.com.SistemaChamados.Controller;
using br.com.SistemaChamados.Model;
using br.com.SistemaChamados.Enum;
using System.Web.Security;
using br.com.SistemaChamados.Controller.Common;
using br.com.SistemaChamados.Constants;
using br.com.SistemaChamados.Util;


public partial class Pages_AddCity : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        ViewStateUserKey = Session.SessionID;
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        br.com.SistemaChamados.Model.Common.User loggedUser = new br.com.SistemaChamados.Model.Common.User();
        UserController userController = new UserController();
        if (Session[UserConstants.SESSION_USER_LOGGED_IN] != null)
        {
            loggedUser.Id = Convert.ToInt32(Session[UserConstants.SESSION_USER_LOGGED_IN]);
            loggedUser = userController.GetUser(loggedUser);
            this.MasterPageFile = MasterPageDefiner.defineMasterPageByAccessRole(loggedUser.Profile);
            ((Label)Master.FindControl("lblUser")).Text = loggedUser.Name;
            this.Title = "SysCall - Sistema de chamados [logado como " + loggedUser.Name + "]";
        }
        else
        {
            Response.Redirect("~/Pages/Login/Login.aspx");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.IsInRole("ADMIN"))
        {
            txbDescricao.Focus();
            if (!IsPostBack)
            {
                FillDdl();
            }
        }
        else
        {
            FormsAuthentication.SignOut();
            Response.Clear();
            Response.Redirect("~/Pages/Login/Login.aspx");
        }
    }

    protected void btnCadastrar_Click(object sender, EventArgs e)
    {
        City city = new City();
        CityController cityController = new CityController();
        State state = null;
        try
        {
            state = new State();
            state.Id = Convert.ToInt32(ddlStates.SelectedValue);
            city.Description = txbDescricao.Text;
            city.State = state;
            EnumCRUDState insertOperationResult = cityController.addCity(city);
            if (insertOperationResult == EnumCRUDState.SQL_SUCCESS)
            {
                Response.Write(@"<script language='javascript'>alert('Cidade cadastrada com sucesso!');</script>");
                ClearFields();
            }
            else
            {
                Response.Write(@"<script language='javascript'>alert('Ocorreu um erro durante o cadastro, por favor tente novamente!');</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write(@"<script language='javascript'>alert('Ocorreu um erro ao criar a cidade: <br>" + ex.Message +".');</script>");
            city = null;
            cityController = null;
        }
    }

    private void ClearFields()
    {
        txbDescricao.Text = "";
        for (int i = 0; i < ddlStates.Items.Count; i++)
        {
            ddlStates.Items[i].Selected = false;
        }
    }
    private void FillDdl()
    {
        StateController stateController = new StateController();
        ddlStates.DataSource = stateController.GetStates();
        ddlStates.DataTextField = "Acronym";
        ddlStates.DataValueField = "Id";
        ddlStates.DataBind();
        ddlStates.Items.Insert(0, "Selecione");
    }
}